package component.security.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * 
 * 文件工具里
 */

public class FileUtil {


	  /**
	   * 从文件读取对象
	   * @preserve
	   * @param filename 指定的文件路径
	   * @return 从文件获取的对象
	   * @throws java.lang.ClassNotFoundException 类型转换出错
	   * @throws FileNotFoundException 指定文件路径不存在
	   * @throws IOException 执行文件读操作失败
	   */
	  public static Object readObjectFromFile(String filename) throws
	      ClassNotFoundException, FileNotFoundException, IOException {
	    return readObjectFromFile(new File(filename));
	  }
	  
	  /**
	   * 从文件读取内容
	   * @preserve
	   * @param filename 指定的文件路径
	   * @return 从文件获取的对象
	   * @throws java.lang.ClassNotFoundException 类型转换出错
	   * @throws FileNotFoundException 指定文件路径不存在
	   * @throws IOException 执行文件读操作失败
	   */
	  public static String readStringFromFile(String filename, String splitStr) throws
	      ClassNotFoundException, FileNotFoundException, IOException {
		  
		  BufferedReader read = new BufferedReader(new FileReader(filename));
		  StringBuffer buffer = new StringBuffer();
		  String line;
		  while(( line = read.readLine() ) != null){
			  buffer.append(line).append(splitStr);
		  }
		  read.close();
		  return buffer.toString();
	  }
	  
	  /**
	   * 从文件读取字节
	   * @preserve
	   * @param filename 指定的文件路径
	   * @return 从文件获取的对象
	   * @throws java.lang.ClassNotFoundException 类型转换出错
	   * @throws FileNotFoundException 指定文件路径不存在
	   * @throws IOException 执行文件读操作失败
	   */
	  public static byte[] readByteFromFile(String filename) throws
	      ClassNotFoundException, FileNotFoundException, IOException {
		  
		  FileInputStream fis = new FileInputStream(new File(filename)); 
		  byte[] crypto = new byte[1024];
		  fis.read(crypto);
		  fis.close();	
		  return crypto;
	  }
	  

	  /**
	   * 从文件读取对象
	   * @preserve
	   * @param file 指定的文件对象
	   * @return 从文件获取的对象
	   * @throws java.lang.ClassNotFoundException 类型转换出错
	   * @throws FileNotFoundException 指定文件路径不存在
	   * @throws IOException 执行文件读操作失败
	   */
	  public static Object readObjectFromFile(File file) throws ClassNotFoundException,
	      FileNotFoundException, IOException {

	    ObjectInputStream in =
	        new ObjectInputStream(new FileInputStream(file));
	    Object obj = in.readObject();
	    in.close();
	    return obj;
	  }
	  

		/**
	   * 将对象保存到文件
	   * @preserve
	   * @param obj 要保存的对象
	   * @param filename 指定的保存文件路径
	   * @throws FileNotFoundException 指定的文件路径不存在
	   * @throws IOException 执行文件写操作出错
	   */
	  public static void saveObject2File(Object obj, String filename) throws
	      FileNotFoundException, IOException {
	    ObjectOutputStream out =
	        new ObjectOutputStream(new FileOutputStream(filename));
	    out.writeObject(obj);
	    out.flush();
	    out.close();
	  }
	  
	  /**
	   * 将内容写入文件
	   * @preserve
	   * @param filename 指定的文件路径
	   * @throws java.lang.ClassNotFoundException 类型转换出错
	   * @throws FileNotFoundException 指定文件路径不存在
	   * @throws IOException 执行文件读操作失败
	   */
	  public static void writeString2File(String filename, String content) throws
	      ClassNotFoundException, FileNotFoundException, IOException {
		  File file =  new File(filename);
		  if (!file.exists()){
			file.createNewFile();
		  }		  
		  BufferedWriter write = new BufferedWriter(new FileWriter(filename));
		  write.write(content);		
		  write.flush();
		  write.close();
	  }
	  
	  /**
	   * @preserve
	   * 获取文件路径
	   * @return
	   */
	  public static String getFileDir(String fileName){		
		  String path   = new FileUtil().getClass().getResource("/" + Constant.FILE_DIR +"/" + Constant.DEFAULT_FILE).getPath();
		  if (path!=null){
			  int iIndex = path.lastIndexOf("/");
			  if (iIndex >0){		  
				  return path.substring(0,iIndex) + File.separator +  fileName;
			  } else{
				  return path;
			  }
		  } else {
			  return "";
		  }
	  }
	  
	  public static void main(String[] args) {
		  System.out.println(FileUtil.getFileDir(Constant.PRIKEY_FILE)) ;
	}
		  
}
